package com.fast.develop.dm.core.data;



import java.io.Serializable;

/**
 * @author 莫寅
 * @createTime 2022-06-05 20:26
 * @desc：
 */
public class Order implements Serializable {

    private final Direction direction;
    private final String property;
    public static final Direction DEFAULT_DIRECTION = Direction.ASC;

    public Order(Direction direction, String property) {
        this.direction = direction;
        this.property = property;
    }

    public static Order by(String property) {
        return new Order(DEFAULT_DIRECTION, property);
    }

    public static Order asc(String property) {
        return new Order(Direction.ASC, property);
    }

    public static Order desc(String property) {
        return new Order(Direction.DESC, property);
    }

    /**
     * Returns the order the property shall be sorted for.
     *
     * @return
     */
    public Direction getDirection() {
        return direction;
    }

    /**
     * Returns the property to order for.
     *
     * @return
     */
    public String getProperty() {
        return property;
    }

    /**
     * Returns whether sorting for this property shall be ascending.
     *
     * @return
     */
    public boolean isAscending() {
        return this.direction.isAscending();
    }

    /**
     * Returns whether sorting for this property shall be descending.
     *
     * @return
     * @since 1.13
     */
    public boolean isDescending() {
        return this.direction.isDescending();
    }


    /**
     * Returns a new {@link Order} with the given {@link Direction}.
     *
     * @param direction
     * @return
     */
    public Order with(Direction direction) {
        return new Order(direction, this.property);
    }

    /**
     * Returns a new {@link Order}
     *
     * @param property must not be {@literal null} or empty.
     * @return
     * @since 1.13
     */
    public Order withProperty(String property) {
        return new Order(this.direction, property);
    }

    /**
     * Returns a new {@link Sort} instance for the given properties.
     *
     * @param properties
     * @return
     */
    public Sort withProperties(String... properties) {
        return Sort.by(this.direction, properties);
    }

}
